<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="商户名称">
          <el-input v-model="form.merchant_name" placeholder="商户名称" />
        </el-form-item>
        <el-form-item label="卡片ID">
          <el-input v-model="form.card_id" placeholder="卡片ID" />
        </el-form-item>
        <el-form-item label="卡号后四位">
          <el-input v-model="form.card_number" placeholder="卡号后四位" />
        </el-form-item>
        <el-form-item label="交易ID">
          <el-input v-model="form.transaction_id" placeholder="交易ID" />
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="form.status" placeholder="状态" />
        </el-form-item>
        <el-form-item label="币种">
          <el-input v-model="form.currency" placeholder="币种" />
        </el-form-item>
        <el-form-item label="交易类型">
          <el-input v-model="form.transaction_type" placeholder="交易类型" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column
            prop="card_id"
            label="卡片ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="transaction_id"
            label="交易ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="billing_amount"
            label="计费金额"
            align="center"
          >
            <template #default="scope">
              {{ scope.row.billing_amount }} {{ scope.row.billing_currency }}
            </template>
          </el-table-column>
          <el-table-column
            prop="transaction_amount"
            label="交易金额"
            align="center"
          >
            <template #default="scope">
              {{ scope.row.transaction_amount }}
              {{ scope.row.transaction_currency }}
            </template>
          </el-table-column>
          <el-table-column
            prop="transaction_date"
            label="交易时间"
            align="center"
          />
          <el-table-column
            prop="transaction_type"
            label="交易类型"
            align="center"
          />
          <el-table-column prop="status" label="状态" align="center" />
          <el-table-column
            prop="network_transaction_id"
            label="支付网络ID"
            align="center"
          />
          <el-table-column
            prop="masked_card_number"
            label="卡号"
            align="center"
          />
          <el-table-column prop="card_nickname" label="卡昵称" align="center" />
          <el-table-column prop="auth_code" label="授权码" align="center" />
          <el-table-column
            prop="posted_date"
            label="交易发布日期"
            align="center"
          />
          <el-table-column label="商户" align="center">
            <template #default="scope">
              {{ scope.row.name }}-{{ scope.row.category_code }}/{{
                scope.row.country
              }}/{{ scope.row.city }}
            </template>
          </el-table-column>
          <el-table-column
            prop="retrieval_ref"
            label="交易参考号"
            align="center"
          />
          <el-table-column
            prop="failure_reason"
            label="失败原因"
            align="center"
          />
          <el-table-column prop="client_data" label="客户数据" align="center" />
          <el-table-column
            prop="matched_authorizations"
            label="授权ID列表"
            align="center"
          />
          <el-table-column prop="description" label="备注" align="center" />
          <el-table-column
            prop="createtime"
            label="时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, formatter, exportOrDownload } from "@/utils/utils";
import { cardsTransactionsApi } from "@/api/ExAssistant/cards";
const loading = ref(false);
const tableData = ref([]);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  card_id: "",
  card_number: "",
  status: "",
  transaction_id: "",
  transaction_type: "",
  currency: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    card_id: form.card_id,
    card_number: form.card_number,
    status: form.status,
    transaction_id: form.transaction_id,
    transaction_type: form.transaction_type,
    currency: form.currency,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await cardsTransactionsApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await cardsTransactionsApi(data, "blob");
  exportOrDownload(res, "PayFaz-虚拟卡交易记录列表");
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
